﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using ThousandFunClient.model;
using System.Windows.Forms;

namespace ThousandFunClient.Util
{
    class PortUtil
    {
        static public void OpenCOM(SerialPort serialPortMe)
        {
            if (!serialPortMe.IsOpen)
            {
                serialPortMe.Open();
            }
        }
        static public void CloseCom(SerialPort serialPortMe)
        {
            if (serialPortMe.IsOpen)
            {
                serialPortMe.Close();
            }
        }


        public static void SetPortByConfig(SerialPort sp,PortConfig pc)
        {
            //MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            //DialogResult result;
            //if (sp.IsOpen)
            //{
            //    result = MessageBox.Show("请问需要关闭", sp.PortName + "是打开的", buttons);
            //}
            //if (result == DialogResult.Yes)
            //{
            //    return;
            //}

            sp.PortName = pc.PortName;
            sp.BaudRate = pc.BaudRate == "" ? sp.BaudRate :int.Parse(pc.BaudRate);
            sp.Parity = pc.Parity ==""? sp.Parity:(Parity)Enum.Parse(typeof(Parity), pc.Parity);
            sp.DataBits = pc.DataBits==""? sp.DataBits:int.Parse(pc.DataBits);
            sp.StopBits = pc.StopBits==""? sp.StopBits:(StopBits)Enum.Parse(typeof(StopBits), pc.StopBits);
            sp.Handshake = pc.Handshake==""? sp.Handshake:(Handshake)Enum.Parse(typeof(Handshake), pc.Handshake);
            sp.ReadTimeout = pc.ReadTimeout==""? 500:Int32.Parse(pc.ReadTimeout);
            
        }


        public static string SetPortName(string defaultPortName)
        {
            string portName;

            Console.WriteLine("Available Ports:");
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("COM port({0}): ", defaultPortName);
            portName = Console.ReadLine();

            if (portName == "")
            {
                portName = defaultPortName;
            }
            return portName;
        }

        public static int SetPortBaudRate(int defaultPortBaudRate)
        {
            string baudRate;

            Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();

            if (baudRate == "")
            {
                baudRate = defaultPortBaudRate.ToString();
            }

            return int.Parse(baudRate);
        }

        public static Parity SetPortParity(Parity defaultPortParity)
        {
            string parity;

            Console.WriteLine("Available Parity options:");
            foreach (string s in Enum.GetNames(typeof(Parity)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Parity({0}):", defaultPortParity.ToString());
            parity = Console.ReadLine();

            if (parity == "")
            {
                parity = defaultPortParity.ToString();
            }

            return (Parity)Enum.Parse(typeof(Parity), parity);
        }

        public static int SetPortDataBits(int defaultPortDataBits)
        {
            string dataBits;

            Console.Write("Data Bits({0}): ", defaultPortDataBits);
            dataBits = Console.ReadLine();

            if (dataBits == "")
            {
                dataBits = defaultPortDataBits.ToString();
            }

            return int.Parse(dataBits);
        }

        public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
        {
            string stopBits;

            Console.WriteLine("Available Stop Bits options:");
            foreach (string s in Enum.GetNames(typeof(StopBits)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
            stopBits = Console.ReadLine();

            if (stopBits == "")
            {
                stopBits = defaultPortStopBits.ToString();
            }

            return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
        }

        public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
        {
            string handshake;

            Console.WriteLine("Available Handshake options:");
            foreach (string s in Enum.GetNames(typeof(Handshake)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString());
            handshake = Console.ReadLine();

            if (handshake == "")
            {
                handshake = defaultPortHandshake.ToString();
            }

            return (Handshake)Enum.Parse(typeof(Handshake), handshake);
        }
    }
}
